#!/bin/sh /etc/rc.common
# XMPP client software
# Copyright (C) 2015-2019 iopsys Software Solutions AB
# Author: Amin Ben Ramdhane <amin.benramdhane@pivasoftware.com>

START=99
STOP=10

USE_PROCD=1
PROG="/usr/sbin/icwmp_xmppd"

start_service() {
	local enable_xmpp=`uci -q get cwmp_xmpp.xmpp.enable`
	local id=`uci -q get cwmp_xmpp.xmpp.id`
	local xmpp_connection=`uci show cwmp_xmpp | grep "cwmp_xmpp.@xmpp_connection.*connection_instance=\'$id\'" | cut -d "." -f 2`
	local xmpp_connection_server=`uci show cwmp_xmpp | grep "cwmp_xmpp.@xmpp_connection.*id_connection=\'$id\'" | cut -d "." -f 2`
	local serveralgorithm=`uci get cwmp_xmpp.$xmpp_connection.serveralgorithm`
	local enable_conn=`uci -q get cwmp_xmpp.$xmpp_connection.enable`
	local enable_srv=`uci -q get cwmp_xmpp.$xmpp_connection_server.enable`
	if [ "$enable_xmpp" = "1" ]; then
		if ([ "$serveralgorithm" = "DNS-SRV" ] && [ "$enable_conn" = "1" ]) || ([ "$serveralgorithm" = "ServerTable" ] && [ "$enable_conn" = "1" ] && [ "$enable_srv" = "1" ]); then
			procd_open_instance
			procd_set_param command "$PROG"
			procd_set_param respawn "3" "7" "0"
			procd_close_instance
		fi
	fi
}

boot() {
	start
}

reload_service() {
	stop
	start 
}

service_triggers()
{
	procd_add_reload_trigger cwmp_xmpp
}
